package com.ruoyi.framework.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.config.properties.DruidProperties;
import com.ruoyi.framework.datasource.DynamicDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * druid 配置多数据源
 *
 * @author ruoyi
 */
@Configuration
public class DruidConfig {

    @Bean("masterDataSource")
    @ConfigurationProperties("spring.datasource.druid.master")
    public DataSource masterDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }

    @Bean("slaveDataSource")
    @ConfigurationProperties("spring.datasource.druid.slave")
    @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true")
    public DataSource slaveDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }

    /*@Bean("arrangeDataSource")
    @ConfigurationProperties("spring.datasource.druid.arrange")
    @ConditionalOnProperty(prefix = "spring.datasource.druid.arrange", name = "enabled", havingValue = "true")
    public DataSource arrangeDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }*/


    @Bean("vpaiDataSource")
    @ConfigurationProperties("spring.datasource.druid.vpai")
    @ConditionalOnProperty(prefix = "spring.datasource.druid.vpai", name = "enabled", havingValue = "true")
    public DataSource vpaiDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }

    /*@Bean("workerDataSource")
    @ConfigurationProperties("spring.datasource.druid.worker")
    @ConditionalOnProperty(prefix = "spring.datasource.druid.worker", name = "enabled", havingValue = "true")
    public DataSource workerDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }*/

    /*@Bean("operationDataSource")
    @ConfigurationProperties("spring.datasource.druid.operation")
    @ConditionalOnProperty(prefix = "spring.datasource.druid.operation", name = "enabled", havingValue = "true")
    public DataSource operationDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }*/
    @Bean("recruitDataSource")
    @ConfigurationProperties("spring.datasource.druid.recruit")
    @ConditionalOnProperty(prefix = "spring.datasource.druid.recruit", name = "enabled", havingValue = "true")
    public DataSource recruitDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }

    @Bean("commonDataSource")
    @ConfigurationProperties("spring.datasource.druid.common")
    @ConditionalOnProperty(prefix = "spring.datasource.druid.common", name = "enabled", havingValue = "true")
    public DataSource commonDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }

    @Bean("omsDataSource")
    @ConfigurationProperties("spring.datasource.druid.oms")
    @ConditionalOnProperty(prefix = "spring.datasource.druid.oms", name = "enabled", havingValue = "true")
    public DataSource omsDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }

    @Bean("organizationDataSource")
    @ConfigurationProperties("spring.datasource.druid.organization")
    @ConditionalOnProperty(prefix = "spring.datasource.druid.organization", name = "enabled", havingValue = "true")
    public DataSource organizationDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }

    @Bean("payoffDataSource")
    @ConfigurationProperties("spring.datasource.druid.payoff")
    @ConditionalOnProperty(prefix = "spring.datasource.druid.payoff", name = "enabled", havingValue = "true")
    public DataSource payoffDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }

    @Bean("taskDataSource")
    @ConfigurationProperties("spring.datasource.druid.task")
    @ConditionalOnProperty(prefix = "spring.datasource.druid.task", name = "enabled", havingValue = "true")
    public DataSource taskDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }


    @Bean("userDataSource")
    @ConfigurationProperties("spring.datasource.druid.user")
    @ConditionalOnProperty(prefix = "spring.datasource.druid.user", name = "enabled", havingValue = "true")
    public DataSource userDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }

    @Bean("openapiDataSource")
    @ConfigurationProperties("spring.datasource.druid.openapi")
    @ConditionalOnProperty(prefix = "spring.datasource.druid.openapi", name = "enabled", havingValue = "true")
    public DataSource openapiDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }

    @Bean("attendanceDataSource")
    @ConfigurationProperties("spring.datasource.druid.attendance")
    @ConditionalOnProperty(prefix = "spring.datasource.druid.attendance", name = "enabled", havingValue = "true")
    public DataSource attendanceDataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return druidProperties.dataSource(dataSource);
    }

    @Bean(name = "dynamicDataSource")
    @Primary
    public DynamicDataSource dataSource(@Qualifier("masterDataSource") DataSource masterDataSource,
                                        //@Qualifier("slaveDataSource") DataSource slaveDataSource,
                                        //@Qualifier("arrangeDataSource") DataSource arrangeDataSource,
                                        @Qualifier("commonDataSource") DataSource commonDataSource,
                                        @Qualifier("omsDataSource") DataSource omsDataSource,
                                        @Qualifier("payoffDataSource") DataSource payoffDataSource,
                                        @Qualifier("taskDataSource") DataSource taskDataSource,
                                        //@Qualifier("operationDataSource") DataSource operationDataSource,
                                        @Qualifier("organizationDataSource") DataSource organizationDataSource,
                                        @Qualifier("recruitDataSource") DataSource recruitDataSource,
                                        @Qualifier("userDataSource") DataSource userDataSource,
                                        @Qualifier("openapiDataSource") DataSource openapiDataSource,
                                        @Qualifier("vpaiDataSource") DataSource vpaiDataSource,
                                        @Qualifier("attendanceDataSource") DataSource attendanceDataSource
                                        //@Qualifier("workerDataSource") DataSource workerDataSource,
                                        ) {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
        //targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource);
        //targetDataSources.put(DataSourceType.ARRANGE.name(), arrangeDataSource);
        //targetDataSources.put(DataSourceType.OPERATION.name(), operationDataSource);
        targetDataSources.put(DataSourceType.RECRUIT.name(), recruitDataSource);
        targetDataSources.put(DataSourceType.VPAI.name(), vpaiDataSource);
        //targetDataSources.put(DataSourceType.WORKER.name(), workerDataSource);
        targetDataSources.put(DataSourceType.COMMON.name(), commonDataSource);
        targetDataSources.put(DataSourceType.OMS.name(), omsDataSource);
        targetDataSources.put(DataSourceType.ORGANIZATION.name(), organizationDataSource);
        targetDataSources.put(DataSourceType.PAYOFF.name(), payoffDataSource);
        targetDataSources.put(DataSourceType.TASK.name(), taskDataSource);
        targetDataSources.put(DataSourceType.USER.name(), userDataSource);
        targetDataSources.put(DataSourceType.OPENAPI.name(), openapiDataSource);
        targetDataSources.put(DataSourceType.ATTENDANCE.name(), attendanceDataSource);
        return new DynamicDataSource(masterDataSource, targetDataSources);
    }
}
